import Vue from 'vue'
import iView from 'iview'
import Util from '../libs/util'
import VueRouter from 'vue-router'
import {
  routers
} from './router'

Vue.use(VueRouter)

// 路由配置
const RouterConfig = {
  // mode: 'history',
  routes: routers
}

const router = new VueRouter(RouterConfig)

router.beforeEach((to, from, next) => {
  if (to.name === 'logout') {
    localStorage.removeItem('ticket')
    if (from.name === 'login') return
    next({
      name: 'login'
    })
    return
  } else if (to.name !== 'login' && to.meta.anonymous !== true && localStorage.getItem('ticket') === null) {
    next({
      name: 'login'
    })
    return
  }
  iView.LoadingBar.start()
  Util.title(to.meta.title)
  next()
})

router.afterEach((to) => {
  iView.LoadingBar.finish()
  window.scrollTo(0, 0)
})

export default router
